package com.party.core.service.member.impl;

import com.google.common.base.Strings;
import com.party.common.paging.Page;
import com.party.common.utils.LangUtils;
import com.party.common.utils.StringUtils;
import com.party.core.dao.read.member.MemberGroupReadDao;
import com.party.core.dao.write.member.MemberGroupWriteDao;
import com.party.core.model.BaseModel;
import com.party.core.model.YesNoStatus;
import com.party.core.model.member.MemberGroup;
import com.party.core.model.member.MemberGroupOutput;
import com.party.core.model.member.MemberWithPartner;
import com.party.core.model.member.PartnerInfoDetails;
import com.party.core.service.member.IMemberGroupService;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.collections.map.HashedMap;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * @ClassName MemberGroupService
 * @Description 机构用户
 * @Author yifeng
 * @Date 2019/9/10 17:47
 **/
@Service
public class MemberGroupService implements IMemberGroupService {
    @Autowired
    private MemberGroupReadDao memberGroupReadDao;
    @Autowired
    private MemberGroupWriteDao memberGroupWriteDao;

    @Override
    public String insert(MemberGroup memberGroup) {
        BaseModel.preInsert(memberGroup);
        boolean result = memberGroupWriteDao.insert(memberGroup);
        if (result) {
            return memberGroup.getId();
        }
        return null;
    }

    @Override
    public boolean update(MemberGroup memberGroup) {
        if (memberGroup.getUpdateDate() != null) {
            memberGroup.setUpdateDate(new Date());
        }
        return memberGroupWriteDao.update(memberGroup);
    }

    @Override
    public boolean deleteLogic(String id) {
        if (Strings.isNullOrEmpty(id)) {
            return false;
        }
        return memberGroupWriteDao.deleteLogic(id);
    }

    @Override
    public boolean delete(String id) {
        if (Strings.isNullOrEmpty(id)) {
            return false;
        }
        return memberGroupWriteDao.delete(id);
    }

    @Override
    public boolean batchDeleteLogic(Set<String> ids) {
        if (CollectionUtils.isEmpty(ids)) {
            return false;
        }
        return memberGroupWriteDao.batchDeleteLogic(ids);
    }

    @Override
    public boolean batchDelete(Set<String> ids) {
        if (CollectionUtils.isEmpty(ids)) {
            return false;
        }
        return memberGroupWriteDao.batchDelete(ids);
    }

    @Override
    public MemberGroup get(String id) {
        return memberGroupReadDao.get(id);
    }

    @Override
    public List<MemberGroup> listPage(MemberGroup memberGroup, Page page) {
        return memberGroupReadDao.listPage(memberGroup, page);
    }

    @Override
    public List<MemberGroup> list(MemberGroup memberGroup) {
        return memberGroupReadDao.listPage(memberGroup, null);
    }

    @Override
    public List<MemberGroup> batchList(Set<String> ids, MemberGroup memberGroup, Page page) {
        if (CollectionUtils.isEmpty(ids)) {
            return Collections.EMPTY_LIST;
        }
        return memberGroupReadDao.batchList(ids, new HashedMap(), page);
    }

    @Override
    public MemberGroup getUnique(MemberGroup memberGroup) {
        return memberGroupReadDao.getUnique(memberGroup);
    }

    @Override
    public boolean isChild(String id) {
        MemberGroup memberGroup = memberGroupReadDao.get(id);
        if (StringUtils.isEmpty(memberGroup.getPartnerId())) {
            return false;
        } else {
            return true;
        }
    }

    @Override
    public String getPartnerId(String id) {
        MemberGroup memberGroup = memberGroupReadDao.get(id);
        if (StringUtils.isEmpty(memberGroup.getPartnerId())) {
            return id;
        } else {
            return memberGroup.getPartnerId();
        }
    }

    @Override
    public MemberGroup getPartner(String id) {
        MemberGroup memberGroup = memberGroupReadDao.get(id);
        if (null != memberGroup) {
            if (StringUtils.isEmpty(memberGroup.getPartnerId())) {
                return memberGroup;
            } else {
                return memberGroupReadDao.get(memberGroup.getPartnerId());
            }
        }
        return null;
    }

    @Override
    public List<String> getChildWithMy(String id) {
        List<MemberGroup> memberGroups = memberGroupReadDao.getChildWithMy(id);
        List<String> memberGroupIds = LangUtils.transform(memberGroups, input -> input.getId());
        return memberGroupIds;
    }

    @Override
    public boolean hasChild(String partnerId) {
        List<MemberGroup> childIds = memberGroupReadDao.getChild(partnerId);
        if (childIds.size() > 0) {
            return true;
        } else {
            return false;
        }
    }

    @Override
    public boolean isQueryChild(String memberId) {
        boolean isQueryChild = true;
        boolean isChild = this.isChild(memberId);
        if (isChild) {
            isQueryChild = false;
        } else {
            boolean hasChild = this.hasChild(memberId);
            if (!hasChild) {
                isQueryChild = false;
            }
        }
        return isQueryChild;
    }

    @Override
    public MemberGroup findByPhone(String phone, Integer type) {
        return memberGroupReadDao.findByMobile(phone, type);
    }

    @Override
    public MemberGroup findByLoginName(String loginName, Integer type) {
        return memberGroupReadDao.findByLoginName(loginName, type);
    }

    @Override
    public MemberGroup findByUserName(String userName) {
        return memberGroupReadDao.findByUserName(userName);
    }

    @Override
    public List<MemberGroup> checkUserName(String key, String value, String userId, Integer type) {
        return memberGroupReadDao.checkUserName(value, key, userId, type);
    }

    @Override
    public List<MemberGroupOutput> childListPage(MemberGroup member, Map<String, Object> params, Page page) {
        return memberGroupReadDao.childListPage(member, params, page);
    }

    @Override
    public List<PartnerInfoDetails> getPartnerListData(MemberGroup member, Page page) {
        return memberGroupReadDao.getPartnerListData(member, page);
    }

    @Override
    public List<PartnerInfoDetails> partnerListPage(PartnerInfoDetails member, Map<String, Object> params, Page page) {
        return memberGroupReadDao.partnerListPage(member, params, page);
    }

    @Override
    public List<MemberWithPartner> getAllPartnersSelectlist() {
        return memberGroupReadDao.getAllPartnersSelectlist();
    }

    @Override
    public List<MemberGroup> webListPage(MemberGroup member, Map<String, Object> params, Page page) {
        return memberGroupReadDao.webListPage(member, params, page);
    }

    @Override
    public MemberGroup findByLoginNameOld(String loginName, boolean queryUserName) {
        return memberGroupReadDao.findByLoginNameOld(loginName, queryUserName);
    }

    @Override
    public List<String> getAllPartnerIds() {
        List<MemberGroup> members = getAllPartnerWithName();
        List<String> memberIds = LangUtils.transform(members, input -> input.getId());
        return memberIds;
    }

    @Override
    public List<MemberGroup> getAllPartnerWithName() {
        MemberGroup member = new MemberGroup();
        member.setIsPartner(YesNoStatus.YES.getCode());
        member.setDelFlag(BaseModel.DEL_FLAG_NORMAL);
        member.setOpenStatus(1);
        List<MemberGroup> members = memberGroupReadDao.getPartnerList(member);
        return members;
    }

    @Override
    public MemberGroup getTXZ() {
        return findByUserName("tongxingzhe");
    }

    @Override
    public MemberGroup getThirdMemberManage() {
        return findByUserName("thirdMemberManage");
    }

    @Override
    public List<MemberGroup> getAllPartnerNoOpenStatus() {
        MemberGroup member = new MemberGroup();
        member.setIsPartner(YesNoStatus.YES.getCode());
        member.setDelFlag(BaseModel.DEL_FLAG_NORMAL);
        List<MemberGroup> members = memberGroupReadDao.getPartnerList(member);
        return members;
    }

    @Override
    public List<String> getChild(String partnerId) {
        List<MemberGroup> childLists = memberGroupReadDao.getChild(partnerId);
        List<String> childIds = LangUtils.transform(childLists, input -> input.getId());
        return childIds;
    }

    @Override
    public MemberGroup findByPartnerId(String partnerId, String id) {
        MemberGroup mg = new MemberGroup();
        mg.setId(id);
        mg.setPartnerId(partnerId);
        return memberGroupReadDao.getUnique(mg);
    }

    @Override
    public List<MemberGroup> getChildList(String partnerId) {
        return memberGroupReadDao.getChild(partnerId);
    }

    @Override
    public List<MemberGroup> getChildWithMyAndName(String partnerId) {
        return memberGroupReadDao.getChildWithMy(partnerId);
    }

    @Override
    public List<MemberGroup> getAllChildWithMy(String partnerId) {
        return memberGroupReadDao.getAllChildWithMy(partnerId);
    }

    @Override
    public List<MemberGroup> queryAdminBranchAndXZGSList(String adminId, String partnerId) {
        return memberGroupReadDao.queryAdminBranchAndXZGSList(adminId,partnerId);
    }
    @Override
    public List<String> queryAdminBranchAndXZGSIds(String adminId, String partnerId) {
        List<MemberGroup> childLists = memberGroupReadDao.queryAdminBranchAndXZGSList(adminId,partnerId);
        List<String> childIds = LangUtils.transform(childLists, input -> input.getId());
        return childIds;
    }
}